package edu.pku.sei.modeler.figures;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Ellipse;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.Shape;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;

import edu.pku.sei.modeler.uml.ResourceFactory;

public class UseCaseFigure extends Shape {
	private static final org.eclipse.swt.graphics.Color USECASE_COLOR = ColorConstants.lightBlue;
	
	private Label nameLabel;

	private IFigure contentFigure;
	
//	private Polyline line;
	
	private static final int EXTENSION_POINT_SPACE = 3;
	
	private List<Label> extensionPoints = new ArrayList<Label>();
	
	//TODO: ToolTip is left undone. Label is OK but TextFlow is not.
//	public IFigure description = new TextFlow();
	
	public UseCaseFigure() {
		super();
		XYLayout layout = new XYLayout();
		this.setLayoutManager(layout);

		this.setOpaque(true); // non-transparent figure

		contentFigure = new Ellipse();
		contentFigure.setOpaque(false);
		contentFigure.setForegroundColor(USECASE_COLOR);
		//contentFigure.setBackgroundColor(USECASE_INTERNAL_COLOR);
		this.add(contentFigure);

		nameLabel = new Label();
		//FontData[] fontdata = {new FontData("Arial", 10, SWT.BOLD)};
		nameLabel.setFont(ResourceFactory.getHeadFont());
		nameLabel.setForegroundColor(ColorConstants.black);
	//	nameLabel.setBackgroundColor(ColorConstants.white);
		this.add(nameLabel);
		
		//test
//		((Label)description).setText("a usecase");
		//test
		//TODO: ToolTip is left undone. Label is OK but TextFlow is not.
//		if(((TextFlow)description).getText().equals("")
//				|| ((TextFlow)description).getText().equals(null)) {
//			setToolTip(null);
//		} else {
//			setToolTip(description);			
//		}
	}

	private void makeLabelPositionCenter() {
		Rectangle useCaseArea = this.getBounds();
		int centerX = useCaseArea.x + useCaseArea.width / 2;
		int centerY = useCaseArea.y + useCaseArea.height / 2;

		Dimension labelSize = nameLabel.getPreferredSize();
		nameLabel.setSize(labelSize);
		nameLabel.getBounds().x = centerX - labelSize.width / 2;
		nameLabel.getBounds().y = centerY - labelSize.height / 2;
	}

	public void setExtensionPoints(List<String> extensionPoints) {
		
		for (Iterator<Label> iterator = this.extensionPoints.iterator(); iterator.hasNext();) {
			Label label = (Label) iterator.next();
			this.remove(label);
		}
		this.extensionPoints.clear();
		
//		if (line != null) {
//			this.remove(line);
//		}
//		
//		if (extensionPoints.size() > 0) {
//			line = new Polyline();
//			line.setForegroundColor(ColorConstants.black);
//			line.setLineStyle(2);
//			Rectangle useCaseBound = this.getBounds();
//			line.setStart(new Point(useCaseBound.x, useCaseBound.y - useCaseBound.height/2));
//			line.setEnd(new Point(useCaseBound.x + useCaseBound.width, useCaseBound.y - useCaseBound.height/2));
//			this.add(line);
//			
//		}
		
		for (Iterator<String> iterator = extensionPoints.iterator(); iterator.hasNext();) {
			String string = (String) iterator.next();
			Label label = new Label(string);
			label.setFont(ResourceFactory.getHeadFont());
			label.setForegroundColor(ColorConstants.black);
			this.extensionPoints.add(label);
			this.add(label);
		}
		this.repaint();
	}
	
	public Label getNameLabel() {
		return nameLabel;
	}

	public void setName(String name) {
		this.nameLabel.setText(name);
	}
	
	@Override
	public void setBounds(Rectangle rect) {
		super.setBounds(rect);
		contentFigure.setBounds(rect);
		if (this.extensionPoints.size() == 0) {
			makeLabelPositionCenter();
		}
		else {
			makeLabelPositionTop();
			makeExtensionPointsPosition();
		}
	}

	
	
	
	

	private void makeExtensionPointsPosition() {
		/*
		 * 
		 * the former one is  Rectangle useCaseArea = this.getBounds();
		 */
		Rectangle useCaseArea = this.getBounds().getCopy();
		int x = useCaseArea.x;
		int y = this.nameLabel.getBounds().getBottomLeft().y + 3;
		int height = y;
		for (Iterator<Label> iterator = this.extensionPoints.iterator(); iterator.hasNext();) {
			Label label = (Label) iterator.next();
			label.setLocation(new Point(x, y));
			label.setSize(label.getPreferredSize());
			System.out.println("set label [" + label.getText() + "] "+ new Point(x, y));
			height = y + label.getPreferredSize().height;
			y += EXTENSION_POINT_SPACE + label.getPreferredSize().height;
		}
		height -= useCaseArea.y;
		if (height > useCaseArea.height) {
			useCaseArea.height = height;
			this.setBounds(useCaseArea);
		}
	}

	private void makeLabelPositionTop() {
		Rectangle useCaseArea = this.getBounds();
		int centerX = useCaseArea.x + useCaseArea.width / 2;
		int centerY = useCaseArea.y;

		Dimension labelSize = nameLabel.getPreferredSize();
		nameLabel.setSize(labelSize);
		nameLabel.getBounds().x = centerX - labelSize.width / 2;
		nameLabel.getBounds().y = centerY + 3;
	}

	@Override
	protected void fillShape(Graphics graphics) {

	}

	@Override
	protected void outlineShape(Graphics graphics) {

	}

//	@Override
//	public IFigure getToolTip() {
//		return new Label(String.valueOf(Math.random()));
//	}

	
	
	
}
